/**
 * \class TcpdumpHeader
 *
 * Start Date and Time
 *Duration of captured packet
 *Total packets
 *Average, minimum, and maximum packet sizes.
 */

#include "TcpdumpHeader.h"
#include "iu_wiretap.h"

time_t TcpdumpHeader::lastPacketTime_ = 0;
time_t TcpdumpHeader::lastPacketTimeD_ = 0;
time_t TcpdumpHeader::firstPacketTime_ = 0;
time_t TcpdumpHeader::firstPacketTimeU_ = 0;

int TcpdumpHeader::max_ = 0;
int TcpdumpHeader::min_ = 0;
int TcpdumpHeader::average_ = 0;
int TcpdumpHeader::totalPacket_ = 0;

/**
 * This is a construct for Tcp header
 */
TcpdumpHeader::TcpdumpHeader()
{
  // TODO Auto-generated constructor stub
}
/**
 * This is a destructor for Tcp header
 */
TcpdumpHeader::~TcpdumpHeader()
{
  // TODO Auto-generated destructor stub
}

/**
 * Print start date and time
 */
string
TcpdumpHeader::printTime(const struct pcap_pkthdr *header)
{
  struct tm * timeinfo;
  stringstream ss;
  time_t temp = header->ts.tv_sec;
  timeinfo = localtime(&temp);
  if (timeinfo == NULL)
    {
      perror("localtime");
      exit(EXIT_FAILURE);
    }
  char t[80];
  strftime(t, 80, "%m/%d/%y %H:%M:%S", timeinfo);
  ss<<t;
  return ss.str();
}
/**
 * *Print duration of captured packet
 */
string
TcpdumpHeader::timeDuration(time_t nextPacket, time_t nextPacketU,
    time_t currentPacket, time_t currentPacketU)
{
  stringstream ss;
  char td[100];
  if ((long int) nextPacket == (long int) currentPacket)
    {
      sprintf(td, "%g", (nextPacketU - currentPacketU)/1000000.0);
    }
  else
    {
      //cout<<"nextPacketS"<<nextPacket<<endl;
      //	cout<<"currentPacketS"<<currentPacket<<endl;
      //	cout<<"nextPacketU"<<nextPacketU<<endl;
      //cout<<"currentPacketU"<<currentPacketU<<endl;

      sprintf(td, "%g ", ((nextPacket - currentPacket) * 1000000 + nextPacketU
          - currentPacketU) / 1000000.0);
    }
  ss << td;

  return ss.str();
}
string TcpdumpHeader::getFinalInfo()
{
  stringstream ss;
  ss << "Minimum Packet Size :" << min_ << " Bytes  ";
  ss << "Maximum Packet Size :" << max_ << " Bytes  "<<endl;
  ss << "Average Packet Size :" << (float) average_ / totalPacket_ << endl;
  return ss.str();
}
/*
 *Compute total packets
 *Compute average, minimum, and maximum packet sizes
 */
Protocol
TcpdumpHeader::handlePacket(const struct pcap_pkthdr *header)
{
  stringstream ss;
  string tDuration_;
  totalPacket_++;
  ss<< "Packet No: "<<totalPacket_<<endl;
  ss<< "Start Time :";
  ss<<printTime(header);
  ss<<"."<< (long int) header->ts.tv_usec;

  if ((long int) lastPacketTime_ == 0)
    {
      firstPacketTime_ = header->ts.tv_sec;
      firstPacketTimeU_ = header->ts.tv_usec;
      lastPacketTime_ = header->ts.tv_sec;
      lastPacketTimeD_ = header->ts.tv_usec;
      min_ = header->len;
      max_ = header->len;
    }
  else
    {
      /*
      ss << " End Time :";
      struct tm * timeinfo;
      timeinfo = localtime(&lastPacketTime_);
      if (timeinfo == NULL)
        {
          perror("localtime");
          exit(EXIT_FAILURE);
        }
      char t[80];
      strftime(t, 80, "%m/%d/%y %H:%M:%S", timeinfo);
      ss<<t<<".";
      */
      //ss <<printTime(header);
      ss << (long int) header->ts.tv_usec<<endl;
      tDuration_ = timeDuration(header->ts.tv_sec, header->ts.tv_usec,
          lastPacketTime_, lastPacketTimeD_);
      ss << "Duration From Last Packet :" << setw(10)<<tDuration_;//<<" msec";
    }
  ss << "                     Packet Length :" << header->len <<" bytes"<<endl;

  if (max_ < header->len)
    max_ = header->len;
  else if (min_ > header->len)
    min_ = header->len;

  average_ += (long int) header->len;

  //cout<<"total packet"<<totalPacket_<<endl;

  lastPacketTime_ = header->ts.tv_sec;
  lastPacketTimeD_ = header->ts.tv_usec;
  if(dflag){
    cout<<ss.str();
  }
  return PROTOCOL_MAC;

}
